<template>
  <div class="app-container">
    <wms-form v-model:query-params="queryParams" :form-list="formList" @query="getList" @export="handleExport">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wms:cont:add']">新增</el-button>
      </el-col>
    </wms-form>
    <wms-table :loading="loading" :data-list="contList" :column="column" :pageInfo="queryParams" @get-list="getList"
      @selection-change="handleSelectionChange" />

    <!-- 添加或修改容器资料对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="contRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="容器编号" prop="contNo">
          <el-input v-model="form.contNo" placeholder="请输入容器编号" />
        </el-form-item>
        <el-form-item label="容器名称" prop="contName">
          <el-input v-model="form.contName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="容器类型" prop="contType">
          <el-select v-model="form.contType" placeholder="请选择容器类型">
            <el-option v-for="dict in wms_cont_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="comments">
          <el-input v-model="form.comments" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="cont">
import WmsTable from '@/components/WmsTable/index.vue'
import WmsForm from '@/components/WmsForm/index.vue'
import {
  listCont,
  getCont,
  delCont,
  addCont,
  updateCont,
} from "@/api/wms/cont";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

import { data, column, formList } from './index.js'
const { proxy } = getCurrentInstance();
const queryParams = reactive(data.queryParams);
const contList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");
/** 查询容器资料列表 */
function getList() {
  loading.value = true;
  listCont(proxy.addDateRange(queryParams, queryParams.dateRange)).then(
    (response) => {
      contList.value = response.rows;
      queryParams.total = response.total;
      loading.value = false;
    }
  );
}

// 取消按钮
function cancel() {
  open.value = false;

  reset();
}

// 表单重置
function reset() {
  form.value = {
    contId: null,
    contNo: null,
    contName: null,
    contType: null,
    status: null,
    createName: null,
    comments: null,
  };
  proxy.resetForm("contRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.contId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加容器资料";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _contId = row.contId || ids.value;
  getCont(_contId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改容器资料";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["contRef"].validate((valid) => {
    if (valid) {
      if (form.value.contId != null) {
        updateCont(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCont(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _contIds = row.contId || ids.value;
  proxy.$modal
    .confirm('是否确认删除容器资料编号为"' + _contIds + '"的数据项？')
    .then(function () {
      return delCont(_contIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "wms/cont/export",
    {
      ...queryParams,
    },
    `cont_${new Date().getTime()}.xlsx`
  );
}
getList();
</script>
